package com.mornd.gulimall.ware.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mornd.gulimall.common.core.utils.BigDecimals;
import com.mornd.gulimall.ware.entity.Purchase;
import com.mornd.gulimall.ware.entity.WareInfo;
import com.mornd.gulimall.ware.mapper.PurchaseMapper;
import com.mornd.gulimall.ware.service.IPurchaseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mornd.gulimall.ware.service.IWareInfoService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

import static com.mornd.gulimall.common.core.utils.BigDecimals.setScale;
import static com.mornd.gulimall.ware.enums.PurchaseStatus.ASSIGNED;
import static com.mornd.gulimall.ware.enums.PurchaseStatus.NEW;

/**
 * <p>
 * 采购信息 服务实现类
 * </p>
 *
 * @author mornd
 * @since 2023-07-29
 */
@Service
public class PurchaseServiceImpl extends ServiceImpl<PurchaseMapper, Purchase> implements IPurchaseService {
    @Resource
    private IWareInfoService wareInfoService;

    @Override
    public IPage<Purchase> list(Purchase purchase) {
        IPage<Purchase> page = new Page<>(purchase.validPageNum(), purchase.validPageSize());
        LambdaQueryWrapper<Purchase> qw = Wrappers.<Purchase>lambdaQuery().eq(purchase.getStatus() != null, Purchase::getStatus, purchase.getStatus())
                .eq(purchase.getWareId() != null, Purchase::getWareId, purchase.getWareId())
                .orderByAsc(Purchase::getPriority);
        String key = purchase.getKey();
        if (StringUtils.hasText(key)) {
            qw.and(wrapper -> {
                wrapper.eq(Purchase::getId, key)
                        .or().eq(Purchase::getAssigneeId, key);
            });
        }
        super.page(page, qw);

        for (Purchase record : page.getRecords()) {
            if (record.getWareId() != null) {
                WareInfo ware = wareInfoService.getById(record.getWareId());
                if (ware != null) {
                    record.setWareName(ware.getName());
                }
            }
        }
        return page;
    }

    @Override
    public boolean save(Purchase entity) {
        entity.setId(null);
        entity.setAmount(setScale(entity.getAmount()));
        if (StringUtils.hasText(entity.getAssigneeId())) {
            entity.setStatus(ASSIGNED.ordinal());
        } else {
            entity.setStatus(NEW.ordinal());
        }
        entity.setCreateTime(LocalDateTime.now());
        return super.save(entity);
    }

    @Override
    public boolean updateById(Purchase entity) {
        entity.setAmount(setScale(entity.getAmount()));
        entity.setCreateTime(null);
        if (StringUtils.hasText(entity.getAssigneeId())) {
            entity.setStatus(ASSIGNED.ordinal());
        } else {
            entity.setStatus(NEW.ordinal());
        }
        entity.setUpdateTime(LocalDateTime.now());
        return super.updateById(entity);
    }

    /**
     * 获取未接受的采购单 状态为0，1
     *
     * @return
     */
    @Override
    public List<Purchase> unreceive() {
        LambdaQueryWrapper<Purchase> qw = Wrappers.<Purchase>lambdaQuery().in(Purchase::getStatus, NEW.ordinal(), ASSIGNED.ordinal());
        return super.list(qw);
    }
}
